<template>
	<el-container>
		<el-header>
			<div>
				<el-form :inline="true">
					<el-form-item label="项目名称">
						<el-input
							v-model="search.projectName"
							placeholder="项目名称"
							clearable
						></el-input>
					</el-form-item>
					<el-form-item label="项目编码">
						<el-input
							v-model="search.projectCode"
							placeholder="项目编码"
							clearable
						></el-input>
					</el-form-item>
					<el-form-item label="创建时间">
						<el-date-picker
							v-model="createdTime"
							type="datetimerange"
							range-separator="至"
							start-placeholder="开始日期"
							end-placeholder="结束日期"
							align="right"
							format="YYYY-MM-DD HH:mm:ss"
							value-format="YYYY-MM-DD HH:mm:ss"
						>
						</el-date-picker>
					</el-form-item>
					<el-form-item label="更新时间">
						<el-date-picker
							v-model="modifyTime"
							type="datetimerange"
							range-separator="至"
							start-placeholder="开始日期"
							end-placeholder="结束日期"
							align="right"
							format="YYYY-MM-DD HH:mm:ss"
							value-format="YYYY-MM-DD HH:mm:ss"
						>
						</el-date-picker>
					</el-form-item>
				</el-form>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"
					>添加</el-button
				>
				<el-button
					type="danger"
					plain
					icon="el-icon-delete"
					:disabled="selection.length == 0"
					@click="batch_del"
					>批量删除</el-button
				>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-button
						type="primary"
						icon="el-icon-search"
						@click="upsearch"
						>搜索</el-button
					>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:apiObj="apiObj"
				row-key="projectId"
				@selection-change="selectionChange"
				:params="search"
				stripe
			>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column
					label="#"
					type="index"
					width="50"
				></el-table-column>
				<el-table-column
					label="项目名称"
					prop="projectName"
					width="150"
				></el-table-column>
				<el-table-column
					label="项目编码"
					prop="projectCode"
					width="200"
				></el-table-column>
				<el-table-column
					label="排序"
					prop="seqNo"
					width="80"
				></el-table-column>
				<el-table-column label="状态" prop="status" width="80">
					<template #default="scope">
						<el-switch
							v-model="scope.row.disabled"
							@change="changeSwitch($event, scope.row)"
							:loading="scope.row.$switch_status"
							active-value="N"
							inactive-value="Y"
						></el-switch>
					</template>
				</el-table-column>
				<el-table-column
					label="创建时间"
					prop="createdTime"
					width="180"
				></el-table-column>
				<el-table-column
					label="更新时间"
					prop="lastModifiedTime"
					min-width="150"
				></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="right"
					width="170"
				>
					<template #default="scope">
						<el-button-group>
							<el-button
								text
								type="primary"
								size="small"
								@click="table_show(scope.row, scope.$index)"
								>查看</el-button
							>
							<el-button
								text
								type="primary"
								size="small"
								@click="table_edit(scope.row, scope.$index)"
								>编辑</el-button
							>
							<el-popconfirm
								title="确定删除吗？"
								@confirm="table_del(scope.row, scope.$index)"
							>
								<template #reference>
									<el-button text type="primary" size="small"
										>删除</el-button
									>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog
		v-if="dialog.save"
		ref="saveDialog"
		@success="handleSaveSuccess"
		@closed="dialog.save = false"
	></save-dialog>
</template>

<script>
import saveDialog from "./save";

export default {
	name: "projectManage",
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			apiObj: this.$API.project.page,
			selection: [],
			search: {
				projectName: "",
				projectCode: "",
				createStartTime: "",
				createEndTime: "",
				disable: "",
				modifyStartTime: "",
				modifyEndTime: "",
			},
			createdTime: [],
			modifyTime: [],
			pickerOptions: {
				shortcuts: [
					{
						text: "最近一周",
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(
								start.getTime() - 3600 * 1000 * 24 * 7
							);
							picker.$emit("pick", [start, end]);
						},
					},
					{
						text: "最近一个月",
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(
								start.getTime() - 3600 * 1000 * 24 * 30
							);
							picker.$emit("pick", [start, end]);
						},
					},
					{
						text: "最近三个月",
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(
								start.getTime() - 3600 * 1000 * 24 * 90
							);
							picker.$emit("pick", [start, end]);
						},
					},
				],
			},
		};
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open();
			});
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//查看
		table_show(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("show").setData(row);
			});
		},
		//权限设置
		permission() {
			this.dialog.permission = true;
			this.$nextTick(() => {
				this.$refs.permissionDialog.open();
			});
		},
		//删除
		async table_del(row) {
			var reqData = { id: row.projectId };
			var res = await this.$API.project.remove.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//批量删除
		async batch_del() {
			this.$confirm(
				`确定删除选中的 ${this.selection.length} 项吗？`,
				"提示",
				{
					type: "warning",
				}
			)
				.then(() => {
					const loading = this.$loading();
					this.$refs.table.refresh();
					loading.close();
					this.$message.success("操作成功");
				})
				.catch(() => {});
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var reqData = { id: row.projectId, disable: val };
			var res = await this.$API.project.disable.post(reqData);
			if (res.code == 200) {
				this.$message.success("操作成功");
				row.disabled = val;
			} else this.$message.error("操作失败");
			delete row.$switch_status;
		},
		//搜索
		upsearch() {
			if (this.createdTime != null && this.createdTime.length > 0) {
				this.search.createStartTime = this.createdTime[0];
				this.search.createEndTime = this.createdTime[1];
			} else {
				this.search.createStartTime = "";
				this.search.createEndTime = "";
			}
			if (this.modifyTime != null && this.modifyTime.length > 0) {
				this.search.modifyStartTime = this.modifyTime[0];
				this.search.modifyEndTime = this.modifyTime[1];
			} else {
				this.search.modifyStartTime = "";
				this.search.modifyEndTime = "";
			}
			this.$refs.table.upData(this.search);
		},
		//本地更新数据
		handleSaveSuccess(data, mode) {
			if (mode == "add") {
				this.$refs.table.refresh();
			} else if (mode == "edit") {
				this.$refs.table.refresh();
			}
		},
	},
};
</script>

<style></style>
